package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Exame;

public class DAOExame implements InterfaceDAOExame{
	
	
	public void createExame(Exame exame, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Exame "
				+ "(CodUsuario, DataExame, Hospital_Clinica, Medico, TipoExame, ResultadoExame) "
				+ "values (?,?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, exame.getDataExame());
			stmp.setString(3, exame.getHospitalClinica());
			stmp.setString(4, exame.getMedico());
			stmp.setString(5, exame.getTipoExame());
			stmp.setString(6, exame.getResultado());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	
	public ArrayList<Exame> readExame(Integer cod) throws SQLException {
		Connection con = new ConnectionFactory().getConnection();
		String seleciona = " where CodUsuario="+Integer.toString(cod);
		String sql = "Select * from Exame"+seleciona;

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Exame> lista = new ArrayList<Exame>();
			while (rs.next()) {
				Exame exame = new Exame();
				exame.setDataExame(rs.getString("DataExame"));
				exame.setHospitalClinica(rs.getString("Hospital_Clinica"));
				exame.setMedico(rs.getString("Medico"));
				exame.setTipoExame(rs.getString("TipoExame"));
				exame.setResultado(rs.getString("ResultadoExame"));
				exame.setCod(rs.getInt("idExame"));
				lista.add(exame);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	
	public void updateExame(Exame exame) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Exame set DataExame=?"
				+ " ,Hospital_Clinica=?"+" ,Medico=?" + " ,TipoExame=?"
				+ " ,ResultadoExame=?"
				+ "where idExame=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, exame.getDataExame());
			stmp.setString(2, exame.getHospitalClinica());
			stmp.setString(3, exame.getMedico());
			stmp.setString(4, exame.getTipoExame());
			stmp.setString(5, exame.getResultado());
			stmp.setInt(6, exame.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	
	public void deleteExame(Exame exame) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Exame where idExame=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, exame.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

}
